#!/usr/bin/php
<?php

$iprntcmd = "/usr/bin/iprntcmd";

$changedQueueMap = array(
	"LibraryRef_Lanier275HDN_PrintVR" => "LibraryRef_Lanier275HDN",
	"Fellows100_LanierLD040B_PrintVR" => "Fellows100_LanierLD040B",
	"CurtisLab_LanierLD040B_PrintVR" => "CurtisLab_LanierLD040B",
	"TaylorHouse_HP4014DN_PrinterVR" => "TaylorHouse_HP4014DN",
	"HuffmanLab_Lanier040B_PrintVR" => "HuffmanLab_Lanier040B"
);


$dnsMap = array(
	"140.141.2.150" => "labprinters.denison.edu"
);



if (!file_exists($iprntcmd) || !is_executable($iprntcmd))
	throw new RuntimeException("'" . $iprntcmd . "' is not a valid binary file.");

// are we on campus while we're running?
exec($iprntcmd . " -L " . escapeshellarg("labprinters.denison.edu"), $output, $retVal);

if (count($output) == 7) // iprntcmd outputs 7 lines of debug code when it can't talk to the server.
	throw new Exception("Can't communicate with labprinters. Bailing out.\n");


// List all locally installed printers and store the raw output in
// the $localPrinters_raw array.
exec($iprntcmd . " -l", $localPrinters_raw, $retVar);

if ($retVar !== 0)
	throw new RuntimeException($iprntcmd . " exited with a non-zero code.");


$localPrinters = array();
$lastQueueName = "";
foreach($localPrinters_raw as $localPrinter)
{
	$parts = explode("=", $localPrinter, 2);
	if (count($parts) != 2)
	{
		continue;
	}

	$key = trim($parts[0]);
	$val = trim($parts[1]);

	if ($key == "installed name")
	{
		// update the queue name if it's one that has changed.
		if (array_key_exists($val, $changedQueueMap))
			$val = $changedQueueMap[$val];

		// store the queue name so on the next pass we'll know where we are in 
		// the array
		$lastQueueName = $val;
		$localPrinters[$val]["queue"] = $val;
	}
	else if ($key == "uri")
	{
		$host = parse_url($val, PHP_URL_HOST);
		if (in_array($host, array_keys($dnsMap)) == FALSE)
		{
			// This isn't a printer that we have in our dns map, so we'll be
			// ignoring it.
			unset($localPrinters[$lastQueueName]);
			$lastQueueName = "";
			continue;
		}
		$localPrinters[$lastQueueName]["host"] = $host;
		$localPrinters[$lastQueueName]["original_uri"] = $val;
		$newUri = "";
		foreach($dnsMap as $ip => $dnsName)
		{
			if (strpos($val, $ip) !== FALSE)
			{
				$newUri = str_replace($ip, $dnsName, $val);
				break;
			}
		}
		echo "Scheduling printer update for '" . $newUri . "'\n";
		$localPrinters[$lastQueueName]["new_uri"] = $newUri;
		$lastQueueName = "";
	}
}



if (count($localPrinters) === 0)
{
	echo "\n\nNo installed printer needs to be modified.\n";
	exit(0);
}



// Now we'll cycle through the printers and delete the ones that we found
foreach($localPrinters as $queue => $printer)
{
	$originalUri = $printer['original_uri'];
	$newUri      = $printer['new_uri'];
	exec("/usr/sbin/lpadmin -x " . escapeshellarg($queue), $output, $retVal);
	//exec($iprntcmd . " -d " . escapeshellarg($originalUri), $output, $retVal);
	if ($retVal !== 0)
	{
		echo "WARNING: Failed to remove '" . $originalUri . "'\n";
	}

	exec($iprntcmd . " -a " . escapeshellarg($newUri), $output, $retVal);
	if ($retVal !== 0)
	{
		echo "WARNING: Failed to install '" . $newUri . "'\n - reinstalling '"  . $originalUri . "'\n";
		exec($iprntcmd . " -a " . escapeshellarg($originalUri), $output, $retVal);
	}
}